<html>
	<head>
		<title>Versions!</title>
		<link rel="stylesheet" href="css/style.css" type="text/css" />
		<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
		<script type="text/javascript" src="js/cufon-yui.js"></script>
		<script type="text/javascript" src="js/WalkwayUltraBold_400.font.js"></script>
		<script type="text/javascript" src="js/WalkwayCondensedBold_400.font"></script>
		<script type="text/javascript">
            Cufon.replace('.title', { fontFamily: 'WalkwayUltraBold'});
		</script>
	</head>
	<body>
		<div class="header"></div>
		<div class="content">
			<p><span class="title">My projects : </span><a href="#" onClick="addClient()"><img src="medias/add.png"  class="icone"/></a></p>
			<ul>
<?php
require_once 'core/tools.php';

$query = "SELECT t1.id_client, t1.client_name, t1.id_projet, t1.projet_name, versions_swf.id_swf, versions_swf.name as swf_name, versions_swf.major, versions_swf.minor, versions_swf.build ";
$query .= "FROM (";
$query .= "SELECT versions_client.id_client, versions_client.name as client_name, versions_projet.id_projet, versions_projet.name as projet_name ";
$query .= "FROM versions_client ";
$query .= "LEFT JOIN versions_projet USING (id_client)";
$query .= ") AS t1 ";
$query .= "LEFT JOIN versions_swf USING (id_projet) ";
$query .= "ORDER BY t1.id_client, t1.id_projet, versions_swf.id_swf";
			
$queryResult = executeSQL($query);

$currentClient = 0;
$currentProjet = 0;


while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC)) {
	
	//on affiche le nom du client
	if ($currentClient != $row["id_client"]) {
		
		if ($currentClient != 0){
			
			if ($currentProjet != 0){
				echo '							</ul>'."\n";
				echo '						</li>'."\n";
			}
			
			echo '					</ul>'."\n";
			echo '				</li>'."\n";
		}
		
		$currentClient = $row["id_client"];
		
		echo '				<li><span class="client">' . $row["client_name"] . '</span><a href="#" onClick="addProjet(' . $row["id_client"] . ')"><img src="medias/add.png" /></a><a href="#" onClick="removeClient(' . $row["id_client"] . ')"><img src="medias/delete.png" /></a>'."\n";
		echo '					<ul>'."\n";
		
		$currentProjet = 0;
		
	}
	
	
	//on affiche le nom du projet
	if ($currentProjet != $row["id_projet"] && !is_null($row["id_projet"])) {
		
		if ($currentProjet != 0){
			echo '							</ul>'."\n";
			echo '						</li>'."\n";
		}
		
		$currentProjet = $row["id_projet"];
		
		echo '						<li>' . $row["projet_name"] .'<a href="#" onClick="addSwf(' . $row["id_projet"] . ')"><img src="medias/add.png" /></a><a href="#" onClick="removeProjet(' . $row["id_projet"] . ')"><img src="medias/delete.png" /></a>'."\n";
		echo '							<ul>'."\n";
	}
	
	//on affiche le nom du swf
	if (!is_null($row["id_swf"])){
		echo '								<li>' . $row["swf_name"] . ' v'.$row["major"].'.'.$row["minor"].'.'.$row["build"].' (id='.$row["id_swf"].')<a href="#" onClick="removeSwf(' . $row["id_swf"] . ')"><img src="medias/delete.png" /></a><a href="#" onClick="showEditPopin('. $row["id_swf"] .')"><img src="medias/bullet_wrench.png" class="icone"/></a></li>'."\n";
	}
	
}

echo '					</ul>'."\n";
echo '				</li>'."\n";


?>				<!--li><span class="client">company</span><img src="medias/add.png" /><img src="medias/delete.png" />
					<ul>
						<li>Salomon Crosschannel <img src="medias/add.png" /><img src="medias/delete.png" />
							<ul>
								<li>Kiosque v1.0.1 (id=12345)<img src="medias/delete.png" /><img src="medias/bullet_wrench.png" class="icone"/></li>
								<li>Wall v1.0.1 (id=12345)<img src="medias/delete.png" /><img src="medias/bullet_wrench.png" class="icone"/></li>
							</ul>
						</li>
						<li>project <img src="medias/add.png" /><img src="medias/delete.png" />
							<ul>
								<li>MainPreloader v1.0.1 (id=12345)<img src="medias/delete.png" /><img src="medias/bullet_wrench.png" class="icone"/></li>
								<li>Main v1.0.1 (id=12345)<img src="medias/delete.png" /><img src="medias/bullet_wrench.png" class="icone"/></li>
								<li>Home v1.0.1 (id=12345)<img src="medias/delete.png" /><img src="medias/bullet_wrench.png" class="icone"/></li>
							</ul>
						</li>
					</ul>
				</li-->
			</ul>
		</div>
		
		<script language="javascript">
		function addClient() {
		
			var client = prompt("Client's name :", "");
			
			if (client != null) {
				
				$.ajax({
					url: 'insert.php',
					type: "POST",
					data: {type: "client", title: client},
					success: function(data) {
						var response = jQuery.parseJSON(data);
						
						if (response.error == 0){
							document.location.reload();
						}
						else {
							console.log('Insertion failed, reason: ' + response.message);
						}

					},
					error: function (jqXHR) {
						console.log("call failed " + jqXHR);
					}

				});

			}
		}
		

		function removeClient(idClient) {
			if (confirm("Are you sure you want to delete this client?")){
				$.ajax({
					url: 'remove.php',
					type: "POST",
					data: {type: "client", id: idClient},
					success: function(data) {
						var response = jQuery.parseJSON(data);
						
						if (response.error == 0){
							document.location.reload();
						}
						else {
							console.log('Remove failed, reason: ' + response.message);
						}
					},
					error: function (jqXHR) {
						console.log("call failed " + jqXHR);
					}

				});
			
			}
		}


		function addProjet(idClient) {

			var projet = prompt("Project's name :", "");
			
			if (projet != null) {
				
				$.ajax({
					url: 'insert.php',
					type: "POST",
					data: {type: "projet", title: projet, id_client: idClient},
					success: function(data) {
						var response = jQuery.parseJSON(data);
						
						if (response.error == 0){
							document.location.reload();
						}
						else {
							console.log('Insertion failed, reason: ' + response.message);
						}

					},
					error: function (jqXHR) {
						console.log("call failed " + jqXHR);
					}

				});

			}
		}

		function removeProjet(idProjet) {
			if (confirm("Are you sure you want to delete this project?")){
				$.ajax({
					url: 'remove.php',
					type: "POST",
					data: {type: "projet", id: idProjet},
					success: function(data) {
						var response = jQuery.parseJSON(data);
						
						if (response.error == 0){
							document.location.reload();
						}
						else {
							console.log('Remove failed, reason: ' + response.message);
						}
					},
					error: function (jqXHR) {
						console.log("call failed " + jqXHR);
					}

				});
			
			}
		}


		function addSwf(idProjet) {
			var swf = prompt("Filename :", "");
			
			if (swf != null) {
				
				$.ajax({
					url: 'insert.php',
					type: "POST",
					data: {type: "swf", title: swf, id_projet: idProjet},
					success: function(data) {
						var response = jQuery.parseJSON(data);
						
						if (response.error == 0){
							document.location.reload();
						}
						else {
							console.log('Insertion failed, reason: ' + response.message);
						}

					},
					error: function (jqXHR) {
						console.log("call failed " + jqXHR);
					}

				});

			}
		}

		function removeSwf(idSwf) {
			if (confirm("Are you sure you want to delete this file?")){
				$.ajax({
					url: 'remove.php',
					type: "POST",
					data: {type: "swf", id: idSwf},
					success: function(data) {
						var response = jQuery.parseJSON(data);
						
						if (response.error == 0){
							document.location.reload();
						}
						else {
							console.log('Remove failed, reason: ' + response.message);
						}
					},
					error: function (jqXHR) {
						console.log("call failed " + jqXHR);
					}

				});
			
			}
		}
   
		function showEditPopin(idSwf) {
			$.ajax({
				url: 'edit.php',
				data: {id_swf: idSwf},
				success: function(data) {
					 $('#popin').html(data);
					 $('#popin').css("display", "block");
				},
				error: function (jqXHR) {
					console.log("call failed " + jqXHR);
				}

			});
		}
   
		
   
		</script>
		<div id="popin"></div>
	</body>
</html>
